home *** CD-ROM | disk | FTP | other *** search
/ Zoom 2 / Zoom - Release 2 (1996)(Active Software)[!].iso / graphics / icon_related / icontools-2.01 / opticon.rexx < prev    next >
OS/2 REXX Batch file  |  1995-12-22  |  8KB  |  310 lines

  1. /*rx                                                          -*- REXX -*-
  2.  *  OPTICON.REXX
  3.  *
  4.  *  (c)Copyright 1994 by Tobias Ferber,  ukjg@rz.uni-karlsruhe.de
  5.  *
  6.  *  This file is part of the IconTools distribution
  7.  *
  8.  *  IconTools is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published
  10.  *  by the Free Software Foundation; either version 1 of the License,
  11.  *  or (at your option) any later version.
  12.  *
  13.  *  IconTools is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with this program; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23. /* $VER: $Id: opticon.rexx,v 1.4 1995/07/18 23:47:52 tf Exp $ */
  24.  
  25. OPTIONS FAILAT 10
  26.  
  27. pathname = ""
  28. destpath = ""
  29. planes   = 0
  30. pattern  = "#?.info"
  31. tempfile = "T:OptIconTemp." || pragma('Id')
  32. template = "FROM/K/A,TO/K,ALL/S,PAT/K,DEPTH=PLANES/K/A,NOEXPAND/S,SMART/S"
  33. args     = ""
  34. cliopts  = ""
  35. optiargs = ""
  36. lsargs   = ""
  37.  
  38. /* parse args */
  39.  
  40. IF ( ARG() < 1 ) | ( (ARG() = 1) & ARG(1)= '?' )  THEN DO
  41.   OPTIONS PROMPT template': '
  42.   PARSE PULL args
  43.   END
  44. ELSE DO n=1 FOR ARG() /* RXFB_TOKEN for RX ?! */
  45.   ARGS= ARGS || ARG(n)
  46.   END
  47.  
  48. DO WHILE WORDS(args) > 0
  49.   av= next_arg()
  50.   SELECT
  51.  
  52.     /* script args */
  53.  
  54.     WHEN UPPER(av) = "FROM" THEN DO
  55.       IF WORDS(args) > 0 THEN DO
  56.         pathname= next_arg()
  57.         IF WORDS(pathname) < 1 THEN pathname= PRAGMA('D')
  58.         END
  59.       ELSE EXIT bad_args("Missing pathname after FROM keyword")
  60.       END /* FROM */
  61.  
  62.     WHEN UPPER(av) = "TO" THEN DO
  63.       IF WORDS(args) > 0 THEN DO
  64.         destpath= next_arg()
  65.         IF WORDS(destpath) < 1 THEN destpath= PARGMA('D')
  66.         END
  67.       ELSE EXIT bad_args("Missing pathname after TO keyword")
  68.       END /* TO */
  69.  
  70.     /* OptIcon args */
  71.  
  72.     WHEN (UPPER(av) = "DEPTH") | (UPPER(av) = "PLANES") THEN DO
  73.       IF WORDS(args) > 0 THEN planes= next_arg()
  74.       ELSE EXIT bad_args("Missing #of bitplanes " UPPER(av) "keyword")
  75.       IF (LENGTH(planes) > 1) | (LENGTH(COMPRESS(planes,"12345678")) > 0) THEN
  76.         EXIT bad_args("Illegal #of bitplanes:" planes "Should be one of 1,2,...,8.")
  77.       END /* DEPTH=PLANES */
  78.  
  79.     WHEN UPPER(av) = "NOEXPAND" then optiargs = optiargs "NOEXPAND"
  80.     WHEN UPPER(av) = "SMART"    then optiargs = optiargs "SMART"
  81.  
  82.     /* List args */
  83.  
  84.     WHEN UPPER(av) = "ALL" THEN DO
  85.       IF POS("ALL",lsopts) < 1 THEN lsargs = lsargs || " ALL"
  86.       END /* ALL */
  87.  
  88.     WHEN UPPER(av) = "PAT" THEN DO
  89.       IF WORDS(args) > 0 THEN pattern= next_arg()
  90.       ELSE EXIT bad_args("Missing pattern after PAT keyword")
  91.       END /* PAT */
  92.  
  93.     /* illegal args */
  94.  
  95.     OTHERWISE DO
  96.       IF av ~= '?' THEN EXIT bad_args("Unknown keyword" av)
  97.                    ELSE EXIT bad_args("")
  98.       END
  99.  
  100.   END /* SELECT */
  101.  
  102. END /* DO */
  103.  
  104. IF planes = 0 THEN EXIT bad_args("Missing #of bitplanes for DEPTH=PLANES/K/A")
  105.  
  106. CALL PRAGMA('W','N')
  107.  
  108. /* try to get missing pathname */
  109.  
  110. IF (WORDS(pathname) < 1) & (EXISTS('c:RequestFile')) THEN DO
  111.   cwd= PRAGMA('D')
  112.   ADDRESS COMMAND 'RequestFile >' tempfile 'DRAWER "'cwd'" TITLE "Select a path..." DRAWERSONLY NOICONS'
  113.  
  114.   IF OPEN('fp',tempfile,'R') THEN DO
  115.     pathname= STRIP(READLN('fp'),'B','"')
  116.     CALL CLOSE('fp')
  117.     ADDRESS COMMAND 'Delete QUIET FILE' tempfile
  118.     END
  119.   ELSE pathname= ""
  120.   END
  121.  
  122. IF WORDS(pathname) < 1 THEN EXIT bad_args("missing FROM pathname")
  123.  
  124. IF ~EXISTS(pathname) THEN DO
  125.   SAY 'Failed to locate your FROM path "'pathname'"'
  126.   EXIT 10
  127.   END
  128.  
  129. /**/
  130.  
  131. IF ~canexist(destpath) THEN DO
  132.   SAY 'Illegal destination directory "'destpath'"'
  133.   EXIT 10
  134.   END
  135.  
  136. /**/
  137.  
  138. SAY 'Collecting icons ...  Please wait ...'
  139.  
  140. cwd= PRAGMA('D',pathname)
  141. ADDRESS COMMAND 'List FILES PAT' pattern 'LFORMAT "%p%n"' lsargs 'TO "'tempfile'"'
  142. CALL PRAGMA('D',cwd)
  143.  
  144. SIGNAL ON HALT
  145. SIGNAL ON BREAK_C
  146. SIGNAL ON BREAK_D
  147.  
  148. IF ~OPEN('fp',tempfile,'R') THEN DO
  149.   SAY 'Error: could not open temporary file "'tempfile'"'
  150.   EXIT 10
  151.   END
  152.  
  153. DO UNTIL EOF('fp')
  154.   fname= STRIP( READLN('fp') )
  155.   IF WORDS(fname) > 0 THEN DO
  156.     fromfile= tackon(pathname,fname)
  157.  
  158.     IF WORDS(destpath) > 0 THEN DO
  159.       pname= tackon(destpath,pathonly(fname))
  160.  
  161.       IF ~EXISTS(pname) & canexist(pname) THEN DO
  162.         IF POS('m',cliopts) > 0 THEN CALL makepath(pname)
  163.         ELSE DO
  164.           OPTIONS PROMPT 'Destination path "'pname'" does not exist.  Shall I create it? (Y/n/a) '
  165.           PULL yna
  166.           IF LEFT(yna,1) ~= 'N' THEN DO
  167.             CALL makepath(pname)
  168.             IF LEFT(yna,1) = 'A' THEN cliopts = cliopts || 'm'
  169.             END
  170.           END
  171.         IF EXISTS(pname) THEN SAY pname '  [created]'
  172.         END
  173.  
  174.       IF EXISTS(pname) THEN DO
  175.         iconfile= tackon(destpath,fname)
  176.         /*SAY 'Copying' fname 'TO' iconfile*/
  177.         ADDRESS COMMAND 'Copy QUIET FROM' transquote(fromfile) 'TO' transquote(iconfile)
  178.         END
  179.  
  180.       ELSE DO
  181.         SAY 'No such directory "'pname'" ... ' fileonly(fname) 'skipped.'
  182.         iconfile= ""
  183.         END
  184.  
  185.       END
  186.     ELSE iconfile= fromfile
  187.  
  188.     IF WORDS(iconfile) > 0 THEN DO
  189.       SAY '  ' iconfile
  190.       ADDRESS COMMAND 'OptIcon NAME' transquote(iconfile) 'PLANES' planes optiargs
  191.       END
  192.  
  193.     END
  194.  
  195.   END /* DO */
  196.  
  197. CALL CLOSE('fp')
  198. ADDRESS COMMAND 'DELETE QUIET FILE "'tempfile'"'
  199. SAY 'done.'
  200. EXIT
  201.  
  202. /**/
  203.  
  204. bad_args: PROCEDURE EXPOSE template
  205.   PARSE ARG str
  206.   IF WORDS(str) > 0 THEN SAY str
  207.   SAY "Template:" template
  208.   SAY "Usage: rx Opticon.rexx FROM <pathname> [TO <destpath>] [ALL] [PAT <pattern>] PLANES [1..8] [NOEXPAND]"
  209.   RETURN 10
  210.  
  211. /*@*/
  212.  
  213. /* get the next command-line argument from global 'args' string */
  214.  
  215. next_arg: PROCEDURE EXPOSE args
  216.   args= STRIP(args)
  217.   IF LEFT(args,1) = '"' THEN PARSE VAR args '"' a '"' args
  218.                         ELSE PARSE VAR args     a     args
  219.   RETURN STRIP(a,'b','"');
  220.  
  221.  
  222. /* translate '"' into '*"' and '*' into '**' */
  223.  
  224. transquote: PROCEDURE
  225.   PARSE ARG s
  226.   t= s
  227.   q= MAX( LASTPOS('*',s), LASTPOS('"',s) )
  228.   DO WHILE q > 0
  229.     t= INSERT('*',t,q-1,1)
  230.     s= LEFT(s,q-1)
  231.     q= MAX( LASTPOS('*',s), LASTPOS('"',s) )
  232.     END
  233.   RETURN '"' || t || '"'
  234.  
  235.  
  236. /* return the non-file part of a pathname */
  237.  
  238. pathonly: PROCEDURE
  239.   PARSE ARG path
  240.   IF (WORDS(path) > 0) & (RIGHT(path,1) ~= ':') THEN DO
  241.     IF RIGHT(path,1) = '/' THEN path= LEFT(path,LENGTH(path)-1)
  242.     IF LASTPOS('/',path) > LASTPOS(':',path) THEN path= LEFT(path,LASTPOS('/',path)-1)
  243.                                              ELSE path= LEFT(path,LASTPOS(':',path))
  244.     END
  245.   RETURN path
  246.  
  247.  
  248. /* return the file part of a pathname */
  249.  
  250. fileonly: PROCEDURE
  251.   PARSE ARG path
  252.   IF RIGHT(path,1) = '/' THEN PATH= LEFT(path,LENGTH(path)-1)
  253.   p= MAX( LASTPOS(':',path), LASTPOS('/',path) )
  254.   IF(p>0) THEN RETURN substr(path,p+1)
  255.           ELSE RETURN path
  256.  
  257.  
  258. /* concatenate the filename to the pathname and return the resulting string */
  259.  
  260. tackon: PROCEDURE
  261.   PARSE ARG path,file
  262.   DO WHILE LEFT(file,1) = '/'
  263.     file= SUBSTR(file,2)
  264.     path= pathonly(path)
  265.     END
  266.   IF (WORDS(path) > 0) & (RIGHT(path,1) ~= '/') & (RIGHT(path,1) ~= ':') THEN path= path || '/'
  267.   IF (RIGHT(file,1) = '/') THEN file= LEFT(file,LENGTH(file)-1)
  268.   RETURN path || file
  269.  
  270.  
  271. /* create all non-existant directories in a path */
  272.  
  273. makepath: PROCEDURE
  274.   PARSE ARG path
  275.   IF RIGHT(path,1) = '/' THEN path= LEFT(path,LENGTH(path)-1)
  276.   IF ~EXISTS(path) THEN DO
  277.     CALL makepath( pathonly(path) )
  278.     ADDRESS COMMAND 'MakeDir NAME "'path'"'
  279.     END
  280.   RETURN 0
  281.  
  282.  
  283. /*
  284.  * return   1  if the device or volume name in given pathname exists
  285.  *             or if no device or volume was present (current device)
  286.  *          0  if the device or volume name does not exist
  287.  */
  288.  
  289. canexist: PROCEDURE
  290.   PARSE UPPER ARG path
  291.   IF POS(':',path) < 1 THEN RETURN 1 /* current device */
  292.   CALL PRAGMA('W','N')
  293.   RETURN EXISTS( LEFT(path,LASTPOS(':',path)) )
  294.  
  295.  
  296. /* break traps */
  297.  
  298. HALT:
  299. BREAK_C:
  300. BREAK_D:
  301.   SIGNAL OFF HALT
  302.   SIGNAL OFF BREAK_C
  303.   SIGNAL OFF BREAK_D
  304.  
  305.   SAY 'Execution halted.'
  306.   EXIT
  307.  
  308.  
  309. /* EOF */
  310.